import {
	baseUrl
} from "../main";
export function getOne(data) {
	let p = new Promise((resolve, reject) => {
		uni.request({
			url: `${baseUrl}student/GetStudentByPhone`,
			data,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
	return p.then(res => {
		return res
	}).catch(err => {
		return err
	})
}

function getToken() {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'univerify',
			univerifyStyle: {
				"icon": {
					"path": "static/images/Logo.png", // 自定义显示在授权框中的logo，仅支持本地图片 默认显示App logo
					"width": "60px", //图标宽度 默认值：60px
					"height": "60px" //图标高度 默认值：60px
				},
				"otherLoginButton": {
					"visible": false, // 是否显示其他登录按钮，默认值：true
				},
			},
			success(res) { // 登录成功
				resolve(res)
				uni.closeAuthView()
			},
			fail(res) { // 登录失败
				console.log(res.errCode)
				console.log(res.errMsg)
				reject(res)
				uni.closeAuthView()
			}
		})
	}).then(res => {
		return res
	}).catch(err => {
		return null
	})
}
export async function login() {
	let res = await getToken()
	console.log(res);
	if (res == null)
		return null
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'checkIn', // 你的云函数名称
			data: {
				access_token: res.authResult.access_token, // 客户端一键登录接口返回的access_token
				openid: res.authResult.openid // 客户端一键登录接口返回的openid
			}
		}).then(res => {
			resolve(res.result.res.phoneNumber)
		}).catch(err => {
			console.log(err);
			reject(err)
		})
	}).then(res => {
		return res
	}).catch(err => {
		return null
	})
}

export function isCheckIn(data) {
	return new Promise((resolve, reject) => {
		uni.request({
			url:`${baseUrl}student/isHome/${data.sid}`,
			success(res) {
				console.log(res.data);
				resolve(res.data)
			},
			fail(err) {
				console.log(err);
				reject(false)
			}
		})
	}).then(res=>{
		return res
	}).catch(err=>{
		return err
	})
}

export function record(data){
	return new Promise((resolve, reject) => {
		uni.request({
			url:`${baseUrl}student/changeIsHomeById/${data.isHome}/${data.sid}`,
			header:{
				"Content-Type":"application/json;charset=UTF-8"
			},
			success(res) {
				resolve(true)
			},
			fail(err) {
				console.log(err);
				reject(false)
			}
		})
	}).then(res=>{
		return res
	}).catch(err=>{
		return err
	})
}
export function getBill(dorid){
	return new Promise((resolve,reject)=>{
		let buffer = dorid.split("#")
		uni.request({
			url:`${baseUrl}dormitory/getDormitoryById/${buffer[0]}/${buffer[1]}`,
			success(res) {
				resolve(res.data)
			},
			fail(err){
				console.log(err);
				reject(false)
			}
		})
	})
}

export function recharge(dorid,type,money){
	return new Promise((resolve,reject)=>{
		let buffer = dorid.split("#")
		let data = {
			type,
			money
		}
		uni.request({
			url:`${baseUrl}dormitory/rechargeBills/${buffer[0]}/${buffer[1]}`,
			data,
			success(res) {
				resolve(true)
			},
			fail(err){
				resolve(false)
			}
		})
	})
}

export function getApply(sid){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:`${baseUrl}apply/getApplyBySid/${sid}`,
			success(res) {
				resolve(res.data)
			},
			fail(err){
				resolve(false)
			}
		})
	})
}

export function getHomemaster(hmid){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:`${baseUrl}homemaster/getHMById/${hmid}`,
			success(res) {
				resolve(res.data)
			},
			fail() {
				reject(false)
			}
		})
	})
}

export function addApply(apply){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:`${baseUrl}apply/addApply`,
			data:apply,
			success(res) {
				resolve(true)
			},
			fail(err){
				resolve(false)
			}
		})
	})
}

export function cancelApply(id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:`${baseUrl}apply/solvedById/${id}`,
			success(res) {
				resolve(true)
			},
			fail(err){
				resolve(false)
			}
		})
	})
}

export function updateApply(apply){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:`${baseUrl}apply/updata`,
			data:apply,
			success(res) {
				resolve(true)
			},
			fail(err){
				resolve(false)
			}
		})
	})
}